package com.bankLogisticsSys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.bankLogisticsSys.model.Networkinfo;
import com.bankLogisticsSys.model.Userinfo;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ManageUserService {
	
	public boolean insert(Userinfo user){
		Userinfo temp = Userinfo.dao.findFirst("select employeeID from userinfo where employeeID = ?", user.getEmployeeID());
		if(temp == null){
			return user.save();
		}
		return false;
	}
	
	public List<Record> getUserList(String name,int page, int rows, String sort, String order){
		int start = ((page - 1) * rows);
		int end = (page * rows);
		List<Object> para = new ArrayList<>();
		String condition = getCondition(name,sort,order, para);
		para.add(start);
		para.add(end);
		String sql = "SELECT u.id,u.employeeID,if(u.role=1,'银行网点员工',if(u.role=2,'银行分行员工',if(u.role=3,'邮政网点',if(u.role=4,'分拨中心',if(u.role=5,'揽投员',if(u.role=6,'银行管理员',if(u.role=7,'邮政管理员','管理员')))))))role, u.password, u.name,u.telephone,u.department,n.network,if(u.state=0,'在职','离职')state,"
				+ "(select bankType from banktype where id = u.bank) as bank "
				+ "FROM userinfo as u "
				+ "join networkinfo as n "
				+ "ON u.networkid = n.id " + condition
				+ " limit ?,?";
		List<Record> ui = Db.find(sql, para.toArray());
		return ui;
	}
	
	public List<Networkinfo> getNetworkList(){
		List<Networkinfo> ni = Networkinfo.dao.find("select id, network from networkinfo where type in(1,2,3,4)");
		return ni;
	}
	
	public String getCondition(String name,String sort, String order,List<Object> para){
		String condition="";
		if(StringUtils.isNotBlank(name)){
			condition += " and u.name like ?";
			para.add("%"+name+"%");
		}
		condition += " order by " + sort + " " + order;
		return condition;
	}
	
	public String getCondition2(String name,List<Object> para){
		String condition="";
		if(StringUtils.isNotBlank(name)){
			condition += " and name like ?";
			para.add("%"+name+"%");
		}
		return condition;
	}
	public Userinfo getUser(int id){
		List<Userinfo> list = Userinfo.dao.find("select * from userinfo where id = ?", id);
		if (list.size() == 0)
			return new Userinfo();
		else
			return list.get(0);

	}
	
	public Userinfo getUserByOpenid(String openid){
		return Userinfo.dao.findFirst("select * from userinfo where openid = ?", openid);
//		List<Userinfo> list = Userinfo.dao.find("select * from userinfo where openid = ?", openid);
//		if (list.size() == 0)
//			return new Userinfo();
//		else
//			return list.get(0);
	}
	
	public Userinfo getUserByNameAndPass(String employeeID,String password){
		return Userinfo.dao.findFirst("select * from userinfo where employeeID = ? and password = ? and state = 0", employeeID,password);
	}
	
	public long getTotalCount(String name){
		List<Object> para = new ArrayList<>();
		String condition = getCondition2(name, para);
		return Db.queryLong("select count(*) from userinfo where 1=1 "+condition,para.toArray());
	}
	
	public Userinfo getUserByEmployeeID(String employeeID){
		return Userinfo.dao.findFirst("select * from userinfo where employeeID = ? and role in (0,6,7)", employeeID);
	}
	
	
	public boolean delete(Userinfo user){
		Userinfo user2 = Userinfo.dao.findById(user.getId());
		if(Db.queryLong("select count(*) from expressrecord where employeeid = ?", user2.getEmployeeID()) == 0){
			return user.delete();
		}
		return false;
	}
	
	public boolean edite(Userinfo user){
		return user.update();
	}
	
	public boolean isExistEmployeeID(String employeeID){
		return (getUserByEmployeeID(employeeID)!=null);
	}
}
